class Solution
{
    int m, n;
    int dx[4] = { 0, 0, 1, -1 }, dy[4] = { -1, 1, 0, 0 };
    bool vis[101][101];
public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance)
    {
        m = maze.size(), n = maze[0].size();
        queue<pair<int, int>> q;
        q.push({ entrance[0], entrance[1] });
        memset(vis, false, sizeof(vis));
        vis[entrance[0]][entrance[1]] = true;
        int ret = 0;
        while (q.size())
        {
            ret++;
            int sz = q.size();
            for (int i = 0; i < sz; i++)
            {
                auto [a, b] = q.front();
                q.pop();
                for (int j = 0; j < 4; j++)
                {
                    int x = dx[j] + a, y = dy[j] + b;
                    if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && maze[x][y] == '.')
                    {
                        if (x == 0 || y == 0 || x == m - 1 || y == n - 1)
                            return ret;
                        q.push({ x, y });
                        vis[x][y] = true;
                    }
                }
            }
        }
        return -1;
    }
};